package com.framework.base.dao;

import java.util.*;

/**
 * Title: ID管理器 * Description:
 * @version 1.0
 */

public class IDManager {
	
	public IDManager() {
	}
	
	/**
	 * 根据uuid的方式来生成主键 
	 * @return
	 */
	public static String generateID(){
		return UUID.randomUUID().toString();
	}
	/**
	 * 生成单个主键;
	 * 
	 * @param userId
	 *            String
	 * @return String
	 */
	public static  String generateID(String userId) {
		if (userId == null || "".equals(userId))
			userId = "0";
		long curentTime = System.currentTimeMillis();
		String ID = String.valueOf(curentTime) + userId
				+ Math.round(Math.random() * 1000000);
		return ID;
	}
	
	  
	  
	/**
	 * 批量生成主键ID;
	 * 
	 * @param userId
	 *            String
	 * @param num
	 *            int
	 * @return String[]
	 */
	@SuppressWarnings("deprecation")
	public String[] generateIDBatch(String userId, int num) {
		if (num == 0)
			return null;
		String returnValue[] = new String[num];
		if (userId == null || "".equals(userId))
			userId = "0";
		int iUserId = Integer.parseInt(userId);
		Date d = new Date();
		StringBuffer sb = new StringBuffer();
		int year = 1900 + d.getYear() - 2000;
		sb.append(year).append(toValue(d.getMonth() + 1));
		sb.append(toValue(d.getDate())).append(toValue(d.getHours()));
		sb.append(toValue(d.getMinutes())).append(toValue(d.getSeconds()));
		sb.append(iUserId).append(Math.round(Math.random() * 100));
		for (int i = 0; i < num; i++) {
			returnValue[i] = sb.append(i).toString();
		}
		return returnValue;
	}

	/**
	 * 
	 * @param value
	 * @return
	 */
	private static String toValue(int value) {
		String strRet = String.valueOf(value);
		if (value < 10) {
			strRet = "0" + Integer.toString(value);
		}
		return strRet;
	}
	public static void main(String[] args) {
		System.out.println(generateID());
	}
}
